package com.epfresh.supplier.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.TagEntity;
import com.epfresh.supplier.entity.TagPackage;
import com.epfresh.supplier.entity.TagPackageTags;
import com.epfresh.supplier.view.tag.FlowLayout;
import com.epfresh.supplier.view.tag.TagAdapter;
import com.epfresh.supplier.view.tag.TagFlowLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductTagActivity extends BaseActivity {



    private TagFlowLayout tag;
    private TagAdapter<TagPackageTags> adapter;
    private List<TagPackageTags> listTag;
    private TextView tvBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tag);

        toolbarTitle.setText("设置商品标签");
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        initData(savedInstanceState);
        String tagPackageStr = getIntent().getStringExtra("tagPackage");
        if (tagPackageStr != null && !"".equals(tagPackageStr)) {
            TagPackage tagPackage = new Gson().fromJson(tagPackageStr, TagPackage.class);
            if (tagPackage != null){
                listTag = tagPackage.getTags();
            }
        }

        if(listTag == null){
            listTag = new ArrayList<>();
        }
        String tagStr = getIntent().getStringExtra("tags");
        Log.e("tags",tagStr+"");
        if (tagStr != null && !"".equals(tagStr)){
            List<TagEntity> tags = new Gson().fromJson(tagStr,new TypeToken<List<TagEntity>>(){}.getType());
            Map<String,String> map = new HashMap<>();
            if (tags != null ){
                for (int i=0;i<tags.size();i++){
                    TagEntity tagEntity = tags.get(i);
                    if (tagEntity != null)
                        map.put(tagEntity.getId() + "", tagEntity.getId() + "");
                }
            }

            for (int i= 0;i<listTag.size();i++){
                TagPackageTags tagPackageTags = listTag.get(i);
                String id = tagPackageTags.getId();
                String s = map.get(id);
                if (s != null && !"".equals(s)){
                    tagPackageTags.setSelect(true);
                }
            }
        }

        //初始化view
        initView();
        //加载数据


    }

    int tagW ;
    int max = 2;
    private void initView() {

        tag = (TagFlowLayout) findViewById(R.id.t_tag);

        tvBtn = (TextView) findViewById(R.id.tv_btn);


        tvBtn.setOnClickListener(this);

        adapter = new TagAdapter<TagPackageTags>(listTag) {
            @Override
            public View getView(FlowLayout parent, int position, TagPackageTags homeCommTag) {
                View v = LayoutInflater.from(ProductTagActivity.this).inflate(R.layout.product_tag,
                        parent, false);


//                ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) tv.getLayoutParams();
//                lp.width = w;
//                lp.leftMargin = LocalDisplay.dp2px(8);
//                lp.topMargin = LocalDisplay.dp2px(10);
//
//
//                lp.height = LocalDisplay.dp2px(30);
                TextView tv = (TextView) v.findViewById(R.id.tv_tag);
                ImageView ivSelect = (ImageView) v.findViewById(R.id.iv_select);

                String color = homeCommTag.getColor();
                int strokeWidth = LocalDisplay.dp2px(0.75f);     // 边框宽度
                float roundRadius = LocalDisplay.dp2px(8f);;     // 圆角半径
//                int strokeColor = Color.parseColor("#FFFF0000");//边框颜色
                int strokeColor = Color.parseColor(color);//边框颜色
                int fillColor = Color.parseColor("#ffffffff"); //内部填充颜色
                GradientDrawable gd = new GradientDrawable();//创建drawable
                gd.setColor(fillColor);
                gd.setCornerRadius(roundRadius);
                gd.setStroke(strokeWidth, strokeColor);
                gd.setGradientType(GradientDrawable.RECTANGLE);

                tv.setBackgroundDrawable(gd);
                tv.setTextColor(strokeColor);
                if (homeCommTag.isSelect()){
                    ivSelect.setVisibility(View.VISIBLE);
                }else {
                    ivSelect.setVisibility(View.INVISIBLE);
                }

                tv.setText(homeCommTag.getName());
                return v;
            }
        } ;

        tag.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {

                int cnt = 0;
                for (int i=0;i<listTag.size();i++){
                    TagPackageTags cornerTag = listTag.get(i);
                    if (cornerTag.isSelect()){
                        cnt++;
                    }
                }
                if (position < listTag.size()){
                    TagPackageTags cornerTag = listTag.get(position);

//                    Log.e("tag",(!cornerTag.isSelect())+"--"+cnt);
                    if (!cornerTag.isSelect()){
                        if (cnt >= max){
                            toast("最多只能选择"+max + "个标签");
                            return true;
                        }
                    }
                    cornerTag.setSelect(!cornerTag.isSelect());
                    ImageView ivSelect = (ImageView) view.findViewById(R.id.iv_select);


                    if (cornerTag.isSelect()){
                        ivSelect.setVisibility(View.VISIBLE);
                    }else {
                        ivSelect.setVisibility(View.INVISIBLE);
                    }

                }
                return true;
            }
        });

        tag.setAdapter(adapter);

    }

    private void initData(Bundle savedInstanceState) {
        if (savedInstanceState != null) {

            getIntent().putExtra("tags",savedInstanceState.getString("tags"));

        }
    }




    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {

            case R.id.tv_btn:

                save();
                break;
        }
    }

    private void save() {


        List<TagPackageTags> tags = new ArrayList<>();

        for (int i= 0;i<listTag.size();i++){
            TagPackageTags tagPackageTags = listTag.get(i);
            if (tagPackageTags.isSelect()) {
                tagPackageTags.setTagId(tagPackageTags.getId());
                tags.add(tagPackageTags);
            }
        }


//        if (tags.size() == 0){
//            toast("请选择合适的商品标签");
//            return;
//        }

        Intent intent = new Intent();
        intent.putExtra("tags",new Gson().toJson(tags));

        setResult(RESULT_OK,intent);
        finish();
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {

        outState.putString("tags",getIntent().getStringExtra("tags"));

        super.onSaveInstanceState(outState);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0x45 && resultCode == RESULT_OK) {

        }
    }
}
